<?php
$db = new Database();
$fs = new Functions();
$set_date_time = $fs->set_default_timezone_datetime();
$auth = Auth::getAuth('current_user');
$result = array();
error_reporting(E_ALL);
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST)){
    
    if(isset($_GET['action'])){
        
        // Add New Role in the system   
        if($_GET['action'] == "add_campaign"){
            
            if(isset($_POST['save_as_draft'])){
                $status = $_POST['save_as_draft'];
            }else{
                $status = "1";
            }
            
            $Campaign = new Campaign($db,$auth['id']);
            $Campaign->title   =   $_POST['campaign_title'];	
            $Campaign->campaign_list_id   =   $_POST['campaign_id'];
            $Campaign->brand_story = $_POST['brand_story'];
            $Campaign->description   =   $_POST['campaign_description'];
            $Campaign->user_id   =   $auth['id'];
            $Campaign->campaign_year =  date("Y");
            $Campaign->campaign_status_id   =   $status;	
            $Campaign->date_created   =   $set_date_time['date_time'];
            $Campaign->date_modified   =   $set_date_time['date_time'];
            $Campaign->is_active   =   "1";
            $Campaign->save();
            
            $return = array("notification"  =>  "Done.",
                            "msg"           =>  "New campaign was successfully saved.",
                            "form_name"     =>  "#new_campaign",
                            "type"          =>  "campaign");
            
            echo json_encode($return);
            
            $fs->save_audit_logs($db,$auth['id'],"10",TB_CAMPAIGN,$set_date_time['date_time'],$Campaign->id);
        }
        
        else if($_GET['action'] == "update_campaign"){
       
            $decrypted_camp_id = $fs->base_encode_decode("decrypt", $_GET['encrypt_id']);
        
            
        
            $Campaign = new Campaign($db,$decrypted_camp_id);
            $Campaign->title   =   $_POST['campaign_title'];	
            $Campaign->campaign_list_id   =   $_POST['campaign_id'];
            $Campaign->brand_story = $_POST['brand_story'];
            $Campaign->campaign_year =  date("Y");
                if(isset($_POST['save_as_draft'])){
                    $status = $_POST['save_as_draft'];
                    $Campaign->campaign_status_id   =   $status;	
                }else{
                    $Campaign->campaign_status_id   =   $_POST['campaign_status'];	
                }
            $Campaign->description   =   $_POST['campaign_description'];
            $Campaign->user_id   =   $auth['id'];
            $Campaign->date_modified   =   $set_date_time['date_time'];
            $Campaign->update();
            
            $return = array("notification"  =>  "Done.",
                            "msg"           =>  "Campaign was successfully updated.",
                            "form_name"     =>  "#new_campaign",
                            "type"          =>  "campaign");
            
            echo json_encode($return);
        
            $fs->save_audit_logs($db,$auth['id'],"12",TB_CAMPAIGN,$set_date_time['date_time'],$Campaign->id);
        }
        
        else if($_GET['action'] == "new_campaign_version"){
            $quarter = $_POST['quarter'];
            $decrypted_camp_id = $fs->base_encode_decode("decrypt", $_GET['encrypt_id']);
        
            switch($quarter){
                case "q1":
                    $q = "Quarter 1";
                    break;
                case "q2":
                    $q = "Quarter 2";
                    break;
                case "q3":
                    $q = "Quarter 3";
                    break;
                case "q4":
                    $q = "Quarter 4";
                    break;
                case "all":
                    $q = "Full";
                    break;
            }
        
            // validate if campaign has a new version
            $sql = "SELECT * FROM " . $db->tbl_name(TB_CAMPAIGN_VERSION) . "
            WHERE campaign_id = {$db->escape($decrypted_camp_id)} ORDER BY version_id DESC";
            
            $campaing_query  = $db->query($sql,"row");
            $campaing_query_nums  = $db->query($sql,"numrows");
          
            
            if($campaing_query_nums != 0){
                $version = $campaing_query['version'] + 1;
                $Campaign_Version = new Campaign_Version($db,$campaing_query['version_id']);
            }else{
                $version = "1";
                $Campaign_Version = new Campaign_Version($db,"");
            }
           
        
            
            
            $Campaign_Version->campaign_id   =   $decrypted_camp_id;	
            $Campaign_Version->user_id   =   $auth['id'];
            $Campaign_Version->date_created = $set_date_time['date_time'];
            
            $Campaign_Version->version_name   =   "Version " . $version . " " .  $q;
            $Campaign_Version->version   =   $version;
            $Campaign_Version->quarter   =   $quarter;
            $Campaign_Version->title   =   $_POST['campaign_title'];
            $Campaign_Version->campaign_list_id   =   $_POST['campaign_id'];	
            $Campaign_Version->brand_story   =   $_POST['brand_story'];
            $Campaign_Version->description   =   $_POST['campaign_description'];
            $Campaign_Version->is_active   =   "1";
            $Campaign_Version->save();
            
            $return = array("notification"  =>  "Done.",
                            "msg"           =>  "Campaign was successfully updated.",
                            "form_name"     =>  "#new_campaign",
                            "type"          =>  "campaign");
            
            echo json_encode($return);
            
            $fs->save_audit_logs($db,$auth['id'],"22",TB_CAMPAIGN,$set_date_time['date_time'],$Campaign_Version->id);
        
        }
        
        else{
            $this->setLayout();
        }
    }else if(isset($_POST['action'])){
        if($_POST['action'] == "remove_campaign"){
            $dataID = $_POST['dataID'];
            
            $decrypted_camp_id = $fs->base_encode_decode("decrypt", $dataID);
            
            $delete = array("id"    =>  $decrypted_camp_id);
            
            $db->delete($db->tbl_name(TB_CAMPAIGN),$delete);
            
            $result = array("notification"  =>  "Done",
                            "msg"           =>  "Campaign was successfully deleted.");
            
            echo json_encode($result);
            
            $fs->save_audit_logs($db,$auth['id'],"13",TB_CAMPAIGN,$set_date_time['date_time'],$decrypted_camp_id);
            
        }else if($_POST['action'] == "approved_campaign"){
            $dataID = $_POST['dataID'];
            
            $decrypted_camp_id = $fs->base_encode_decode("decrypt", $dataID);
            
            $Campaign = new Campaign($db,$decrypted_camp_id);
            $Campaign->campaign_status_id = "2";
            $Campaign->update();
            
            $result = array("notification"  =>  "Done",
                            "msg"           =>  "Campaign was successfully approved.");
            
            echo json_encode($result);
            
            $fs->save_audit_logs($db,$auth['id'],"18",TB_CAMPAIGN,$set_date_time['date_time'],$decrypted_camp_id);
            
        }else if($_POST['action'] == "move_campaign"){
            $dataID = $_POST['dataID'];
            
            $decrypted_camp_id = $fs->base_encode_decode("decrypt", $dataID);
            
            $Campaign = new Campaign($db,$decrypted_camp_id);
            $Campaign->campaign_list_id = $_POST['next_campaign'];
            $Campaign->update();
            
            $result = array("notification"  =>  "Done",
                            "msg"           =>  "Campaign was successfully moved.");
            
            echo json_encode($result);
            
            $fs->save_audit_logs($db,$auth['id'],"19",TB_CAMPAIGN,$set_date_time['date_time'],$decrypted_camp_id);
            
        }else if($_POST['action'] == "complete_quarter_campaign"){
            $get_all_tr = $_POST['get_all_tr'];
            $quarter = $_POST['data_quarter'];
            $year = date("Y");
            
            
            $ec_query_rows = $db->query("SELECT * FROM " . $db->tbl_name(TB_ENERGY_CHART) . " WHERE name = {$db->escape($quarter . '_' . date('Y'))}
            AND year = {$db->escape($year)}","numrows");
            $ec_query = $db->query("SELECT * FROM " . $db->tbl_name(TB_ENERGY_CHART) . " WHERE name = {$db->escape($quarter . '_' . date('Y'))}
            AND year = {$db->escape($year)}","row");
            if($ec_query_rows != 0){
                $Energy_Chart = new Energy_Chart($db,$ec_query['id']);
                $Energy_Chart->td_arrangement = $get_all_tr;
                $Energy_Chart->update();
                
            }else{
                $Energy_Chart = new Energy_Chart($db,$auth['id']);
                $Energy_Chart->name   =   $quarter . "_" . date("Y");	
                $Energy_Chart->year   =   date("Y");
                $Energy_Chart->td_arrangement = $get_all_tr;
                $Energy_Chart->energy_chart_status   =   "1";	
                $Energy_Chart->date   =   $set_date_time['date_time'];	
                $Energy_Chart->user_id   =   $auth['id'];	
                $Energy_Chart->is_active   =   "1";
                $Energy_Chart->save();
            }
            
                $result = array("notification"  =>  "Done",
                                "msg"           =>  ucwords($quarter) . " was successfully tag as completed.");
            
            echo json_encode($result);
            
            $fs->save_audit_logs($db,$auth['id'],"20",TB_ENERGY_CHART,$set_date_time['date_time'],$Energy_Chart->id);
            
        }else if($_POST['action'] == "final_quarter_campaign"){
            $get_all_tr = $_POST['get_all_tr'];
            $quarter = $_POST['data_quarter'];
            
            switch($quarter){
                case "q1":
                    $q = "Quarter 1";
                    break;
                case "q2":
                    $q = "Quarter 2";
                    break;
                case "q3":
                    $q = "Quarter 3";
                    break;
                case "q4":
                    $q = "Quarter 4";
                    break;
                case "all":
                    $q = "Full";
                    break;
            }
            $year = date("Y");
            
            $ec_query_rows = $db->query("SELECT * FROM " . $db->tbl_name(TB_ENERGY_CHART) . " WHERE name = {$db->escape($quarter . "_" . date("Y"))}
            AND year = {$db->escape($year)}","numrows");
            $ec_query = $db->query("SELECT * FROM " . $db->tbl_name(TB_ENERGY_CHART) . " WHERE name = {$db->escape($quarter . "_" . date("Y"))}
            AND year = {$db->escape($year)}","row");
            if($ec_query_rows != 0){
                $Energy_Chart = new Energy_Chart($db,$ec_query['id']);
                $Energy_Chart->td_arrangement = $get_all_tr;
                $Energy_Chart->energy_chart_status   =   "2";	
                $Energy_Chart->update();
                
            }else{
                $Energy_Chart = new Energy_Chart($db,$auth['id']);
                $Energy_Chart->name   =   $quarter . "_" . date("Y");	
                $Energy_Chart->year   =   date("Y");
                $Energy_Chart->td_arrangement = $get_all_tr;
                $Energy_Chart->energy_chart_status   =   "2";	
                $Energy_Chart->date   =   $set_date_time['date_time'];	
                $Energy_Chart->user_id   =   $auth['id'];	
                $Energy_Chart->is_active   =   "1";
                $Energy_Chart->save();
            }
            
        
                $result = array("notification"  =>  "Done",
                                "msg"           =>  ucwords($quarter) . " was successfully tag as final.");
            
            echo json_encode($result);
            
            $fs->save_audit_logs($db,$auth['id'],"21",TB_ENERGY_CHART,$set_date_time['date_time'],$Energy_Chart->id);
        }
        
        // Multiple Selection
        else if($_POST['action'] == "approved_selected_campaign"){
            $dataID = $_POST['dataID'];
            $split_dataID = explode(",",$dataID);
            foreach($split_dataID as $dataID){
                $decrypted_camp_id = $fs->base_encode_decode("decrypt", $dataID);
                
                $Campaign = new Campaign($db,$decrypted_camp_id);
                $Campaign->campaign_status_id = "2";
                $Campaign->update();
            }
            
            $result = array("notification"  =>  "Done",
                            "msg"           =>  "Selected Campaign was successfully approved.");
            
            echo json_encode($result);
            
            $fs->save_audit_logs($db,$auth['id'],"16",TB_CAMPAIGN,$set_date_time['date_time'],$Campaign->id);
            
        }else if($_POST['action'] == "move_selected_campaign"){
            
            $json_encode = json_decode($_POST['next_campaign']);
            foreach($json_encode as $dataID=>$value){
                $decrypted_camp_id = $fs->base_encode_decode("decrypt", $dataID);
                
                $Campaign = new Campaign($db,$decrypted_camp_id);
                $Campaign->campaign_list_id = $value;
                $Campaign->update();
            }
            
            $result = array("notification"  =>  "Done",
                            "msg"           =>  "Selected Campaign was successfully moved.","asda"=>$_POST['next_campaign']);
            
            echo json_encode($result);
            
            $fs->save_audit_logs($db,$auth['id'],"17",TB_CAMPAIGN,$set_date_time['date_time'],$Campaign->id);
        }
        
        
        else{
            $this->setLayout();
        }
    }else{
        $this->setLayout();
    }
}else{
    $this->setLayout();
}
?>